﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExamQuestionEdit.aspx.cs"
    Inherits="YiShi.Exam.ExamQuestionEdit" %>

<%@ Register TagPrefix="Custom" TagName="QuestionListControl" Src="/Question/QuestionListControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" href="/css/redmond/common.css" type="text/css" media="all" />
    <link rel="stylesheet" href="/css/redmond/jquery-ui-1.8.18.custom.css" type="text/css"
        media="all" />

    <script type="text/javascript" src="/scripts/jquery-1.7.1.min.js"></script>

    <script type="text/javascript" src="/scripts/jquery-ui-1.8.18.custom.min.js"></script>

    <script type="text/javascript" src="/scripts/common.js"></script>

    <script type="text/javascript" src="/scripts/listscript.js"></script>

</head>
<body style="font-size: 9pt;">
    <div align="center">
        <table style="width: 960px">
            <tr>
                <td style="border: 1px solid #c5dbec; background-color: #f5f8f9; font-weight: bold"
                    width="450">
                    试卷
                </td>
                <td style="border: 1px solid #c5dbec; background-color: #f5f8f9; font-weight: bold">
                    操作
                </td>
                <td style="border: 1px solid #c5dbec; background-color: #f5f8f9; font-weight: bold"
                    width="450">
                    问题列表
                </td>
            </tr>
            <tr>
                <td style="border: 1px solid #c5dbec;" valign="top">
                    <div style="text-align: right">
                        <button id="MoveUpButton">
                            上移</button>
                        <button id="MoveDownButton">
                            下移</button>
                    </div>
                    <div style="height: 540px; overflow: auto;">
                        <table>
                            <%=tr %>
                            <%=html %>
                        </table>
                    </div>
                </td>
                <td valign="middle">
                    <button id="AddButton" style="font-size: 5pt;">
                        <span class="ui-icon ui-icon-arrowthick-1-w"></span>
                    </button>
                    <br>
                    <br>
                    <br>
                    <button id="RemoveButton" style="font-size: 5pt;">
                        <span class="ui-icon ui-icon-closethick"></span>
                    </button>
                </td>
                <td style="border: 1px solid #c5dbec;" valign="top">
                    <Custom:QuestionListControl runat="server" Width="450" PerPageRecordCount="4" IsQuery="true"
                        SubmitUrl="ExamQuestionEdit.aspx" />
                </td>
            </tr>
            <tr>
                <td align="center" colspan="3">
                    <button id="CloseButton">
                        关闭</button>
                </td>
            </tr>
        </table>

        <script language="javascript">
            $(function event() {
                //Init
                $("button").button();
                $("#AddButton").click(function() {
                    var $ele = $("input:radio[name='Record']:checked");
                    if ($ele.length == 0) {
                        Alert("请选择要操作的问题！");
                        return;
                    }
                    var questionid = $ele.val();
                    var $tr = $ele.parents("tr:first");
                    var body = $tr.find("div").html();
                    var typename = $tr.children("td:last").text();
                    if ($("[name='Question'][value='" + questionid + "']").length != 0) {
                        Alert("试卷中已含有该问题！");
                        return;
                    }

                    var titletrs = $("[name='TitleTr']");
                    var $titletr;
                    for (var i = 0; i < titletrs.length; i++) {
                        if ($(titletrs[i]).text().indexOf(typename) >= 0) {
                            $titletr = $(titletrs[i]);
                            break;
                        }
                    }
                    var $ele = $titletr;
                    var $next = $ele.next();
                    while ($next && $next.attr("name") != "TitleTr") {
                        $ele = $next;
                        $next = $next.next();
                    }
                    var sortindex = 0;
                    if ($ele.attr("name") == "TitleTr")
                        sortindex = 1000;
                    else
                        sortindex = parseInt($ele.find("input[name='Question']").attr("sortindex")) + 1000;

                    var jsondata = {};
                    jsondata["ActionType"] = "Add";
                    jsondata["Id"] = GetUrlArg("Id");
                    jsondata["QuestionId"] = questionid;
                    jsondata["SortIndex"] = sortindex;
                    $.post("ExamQuestionEdit.aspx", jsondata, function(data) {
                        if (data.IsSuccess == "true") {
                            $new = $("#QuestionTemplate").clone(true).removeAttr("id").css({ display: "" });
                            var $input = $new.find("input[name='Question']");
                            var $div = $new.find("div");
                            $input.val(questionid);
                            $input.attr("sortindex", sortindex);
                            $div.html(body);
                            $new.insertAfter($ele);
                        }
                        else
                            ShowError(data.Message, data.Source, data.StackTrace);
                        HideMask();
                    }, "json");
                });
                $("#MoveUpButton").click(function() {
                    var $ele = $("input:radio[name='Question']:checked");
                    if ($ele.length == 0) {
                        Alert("请选择要操作的记录！");
                        return;
                    }
                    var $tr = $ele.parents("tr:first");

                    var $prev = $tr.prev();
                    var $prevradio = $prev.find("input:radio");
                    if ($prevradio.length == 0)
                        return;
                    var sortindex;
                    var $prevprev = $prev.prev();
                    var $prevprevradio = $prevprev.find("input:radio");
                    if ($prevprevradio.length == 0)
                        sortindex = parseInt(parseInt($prevradio.attr("sortindex")) / 2);
                    else
                        sortindex = parseInt((parseInt($prevradio.attr("sortindex")) + parseInt($prevprevradio.attr("sortindex"))) / 2);

                    ShowMask();
                    var jsondata = { "ActionType": "UpdateSortIndex", "Id": GetUrlArg("Id"), "QuestionId": $ele.val(), "SortIndex": sortindex };
                    $.post("ExamQuestionEdit.aspx", jsondata, function(data) {
                        if (data.IsSuccess == "true") {
                            $ele.attr("sortindex", sortindex);
                            $tr.insertBefore($prev);
                        }
                        else
                            ShowError(data.Message, data.Source, data.StackTrace);
                        HideMask();
                    }, "json");
                });
                $("#MoveDownButton").click(function() {
                    var $ele = $("input:radio[name='Question']:checked");
                    if ($ele.length == 0) {
                        Alert("请选择要操作的记录！");
                        return;
                    }
                    var $tr = $ele.parents("tr:first");

                    var $next = $tr.next();
                    var $nextradio = $next.find("input:radio");
                    if ($nextradio.length == 0)
                        return;
                    var sortindex;
                    var $nextnext = $next.next();
                    var $nextnextradio = $nextnext.find("input:radio");
                    if ($nextnextradio.length == 0)
                        sortindex = parseInt($nextradio.attr("sortindex")) + 1000;
                    else
                        sortindex = parseInt((parseInt($nextradio.attr("sortindex")) + parseInt($nextnextradio.attr("sortindex"))) / 2);

                    ShowMask();
                    var jsondata = { "ActionType": "UpdateSortIndex", "Id": GetUrlArg("Id"), "QuestionId": $ele.val(), "SortIndex": sortindex };
                    $.post("ExamQuestionEdit.aspx", jsondata, function(data) {
                        if (data.IsSuccess == "true") {
                            $ele.attr("sortindex", sortindex);
                            $tr.insertAfter($next);
                        }
                        else
                            ShowError(data.Message, data.Source, data.StackTrace);
                        HideMask();
                    }, "json");

                });
                $("#RemoveButton").click(function() {
                    var $ele = $("input:radio[name='Question']:checked");
                    if ($ele.length == 0) {
                        Alert("请选择要操作的记录！");
                        return;
                    }
                    if (!confirm("你确定要删除这条记录么？"))
                        return;
                    var $tr = $ele.parents("tr:first");

                    ShowMask();
                    var jsondata = { "ActionType": "Remove", "Id": GetUrlArg("Id"), "QuestionId": $ele.val() };
                    $.post("ExamQuestionEdit.aspx", jsondata, function(data) {
                        if (data.IsSuccess == "true") {
                            $tr.remove();
                        }
                        else
                            ShowError(data.Message, data.Source, data.StackTrace);
                        HideMask();
                    }, "json");
                });
            });
        </script>

    </div>
</body>
</html>
